﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.CampaignSystem.CharacterDevelopment.CharacterTraits
// Assembly: TaleWorlds.CampaignSystem, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E85F8C15-4DF6-4E9C-A58A-29177E40D07A
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\bin\Win64_Shipping_Client\TaleWorlds.CampaignSystem.dll

using System;
using System.Collections.Generic;
using System.Xml;
using TaleWorlds.Core;
using TaleWorlds.Library;
using TaleWorlds.ObjectSystem;

#nullable disable
namespace TaleWorlds.CampaignSystem.CharacterDevelopment
{
  public class CharacterTraits : PropertyOwner<TraitObject>
  {
    public const int MaxTraitValue = 20;

    internal static void AutoGeneratedStaticCollectObjectsCharacterTraits(
      object o,
      List<object> collectedObjects)
    {
      ((MBObjectBase) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
    {
      base.AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    public CharacterTraits(CharacterTraits propertyOwner)
      : base((PropertyOwner<TraitObject>) propertyOwner)
    {
    }

    public CharacterTraits()
    {
    }

    public int Mercy => this.GetPropertyValue(DefaultTraits.Mercy);

    public int Valor => this.GetPropertyValue(DefaultTraits.Valor);

    public int Honor => this.GetPropertyValue(DefaultTraits.Honor);

    public int Generosity => this.GetPropertyValue(DefaultTraits.Generosity);

    public int Calculating => this.GetPropertyValue(DefaultTraits.Calculating);

    public override void Deserialize(MBObjectManager objectManager, XmlNode node)
    {
      this.Initialize();
      foreach (XmlNode childNode in node.ChildNodes)
      {
        if (childNode.NodeType != XmlNodeType.Comment)
        {
          XmlAttributeCollection attributes = childNode.Attributes;
          string objectName = attributes["id"].Value;
          string str = attributes["value"].Value;
          TraitObject attribute = Game.Current.ObjectManager.GetObject<TraitObject>(objectName);
          if (attribute != null)
          {
            int num = str != null ? Convert.ToInt32(str) : 1;
            this.SetPropertyValue(attribute, num);
          }
        }
      }
      foreach (XmlNode childNode in node.ChildNodes)
      {
        if (childNode.NodeType != XmlNodeType.Comment)
        {
          string objectName = childNode.Name;
          int num = 1;
          if (objectName == "Trait")
          {
            objectName = (string) null;
            if (childNode.Attributes != null)
            {
              XmlAttribute attribute = childNode.Attributes["id"];
              if (attribute != null)
                objectName = attribute.InnerText;
            }
          }
          if (objectName != null)
          {
            TraitObject attribute1 = Game.Current.ObjectManager.GetObject<TraitObject>(objectName);
            if (attribute1 != null)
            {
              XmlAttribute attribute2 = childNode.Attributes["value"];
              if (attribute2 != null)
              {
                string str = attribute2.Value;
                num = str != null ? Convert.ToInt32(str) : 1;
              }
              this.SetPropertyValue(attribute1, num);
            }
          }
        }
      }
    }

    protected override void AfterLoad()
    {
      if (!(MBSaveLoad.LastLoadedGameVersion < ApplicationVersion.FromString("e1.8.1.0")))
        return;
      MBList<KeyValuePair<TraitObject, int>> mbList = this._attributes.ToMBList<KeyValuePair<TraitObject, int>>();
      int val2 = 0;
      foreach (KeyValuePair<TraitObject, int> keyValuePair in (List<KeyValuePair<TraitObject, int>>) mbList)
      {
        if (keyValuePair.Key.StringId == "DesertScoutSkills")
        {
          this._attributes.Remove(keyValuePair.Key);
          val2 = Math.Max(keyValuePair.Value, val2);
        }
        if (keyValuePair.Key.StringId == "WoodsScoutSkills")
        {
          this._attributes.Remove(keyValuePair.Key);
          val2 = Math.Max(keyValuePair.Value, val2);
        }
        if (keyValuePair.Key.StringId == "HillScoutSkills")
        {
          this._attributes.Remove(keyValuePair.Key);
          val2 = Math.Max(keyValuePair.Value, val2);
        }
        if (keyValuePair.Key.StringId == "MarshesLore")
        {
          this._attributes.Remove(keyValuePair.Key);
          val2 = Math.Max(keyValuePair.Value, val2);
        }
        if (keyValuePair.Key.StringId == "SteppeScoutSkills")
        {
          this._attributes.Remove(keyValuePair.Key);
          val2 = Math.Max(keyValuePair.Value, val2);
        }
      }
      if (val2 > 0)
        this._attributes.Add(DefaultTraits.ScoutSkills, val2);
      List<string> source = new List<string>()
      {
        "Engineer",
        "SergeantCommandSkills",
        "Manager",
        "Surgeon",
        "ScoutSkills",
        "Commander",
        "Politician",
        "RogueSkills",
        "Blacksmith"
      };
      foreach (KeyValuePair<TraitObject, int> mb in (List<KeyValuePair<TraitObject, int>>) this._attributes.ToMBList<KeyValuePair<TraitObject, int>>())
      {
        if (source.ContainsQ<string>(mb.Key.StringId) && mb.Value > 0)
          this._attributes[mb.Key] = mb.Value * 2;
      }
    }
  }
}
